package org.lord.slark.numbers.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class STimeUtils {
    private static SimpleDateFormat sdf;

    public static String longToDate(long date) {
        if (date == 0) {
            return "(无)";
        }
        sdf = new SimpleDateFormat("yyyy/M/d");
        Date d = new Date(date);
        return sdf.format(d);
    }

    public static String longToTime(long date) {
        if (date == 0) {
            return "(无)";
        }
        sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }

    public static long dateToLong(String input) {
        if (input == null || input.length() == 0) {
            return 0;
        }
        sdf = new SimpleDateFormat("yyyy/M/d");
        try {
            Date d = sdf.parse(input);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long timeToLong(String input) {
        if (input == null || input.length() == 0) {
            return 0;
        }
        sdf = new SimpleDateFormat("HH:mm");
        try {
            Date d = sdf.parse(input);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String intToWeekDay(int day) {
        String s = null;
        switch (day) {
            case 1:
                s = "主日";
                break;
            case 2:
                s = "星期一";
                break;
            case 3:
                s = "星期二";
                break;
            case 4:
                s = "星期三";
                break;
            case 5:
                s = "星期四";
                break;
            case 6:
                s = "星期五";
                break;
            case 7:
                s = "安息日";
                break;
            default:
                break;
        }
        return s;
    }

    public static int dayBefore(boolean daybeforeYesterday) {
        Calendar c = Calendar.getInstance();
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        if (!daybeforeYesterday) {
            if (currentDay != 1) {
                return currentDay - 1;
            }
            if (currentMonth == 1 || currentMonth == 2 || currentMonth == 4 || currentMonth == 6
                    || currentMonth == 8 || currentMonth == 9 || currentMonth == 11) {
                //腊月
                return 31;
            } else if (currentMonth == 3) {
                //2月
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    //闰年
                    return 29;
                } else {
                    //平年
                    return 28;
                }
            } else {
                return 30;
            }
        } else {
            if (currentDay == 1) {
                return dayBefore(false) - 1;
            } else if (currentDay != 2) {
                return currentDay - 2;
            } else {
                if (currentMonth == 3) {
                    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                        return 29;
                    } else {
                            return 28;
                    }
                } else if (currentMonth == 2 || currentMonth == 4 || currentMonth ==6
                        || currentMonth ==8 || currentMonth == 9
                        || currentMonth == 11 || currentMonth == 1) {
                    return 31;
                } else {
                    return 30;
                }
            }
        }
    }

    public static int dayAfter(boolean dayafterTomorrow) {
        Calendar c = Calendar.getInstance();
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        if (!dayafterTomorrow) {
            if (currentMonth == 2) {
                switch (currentDay) {
                    case 29:
                        return 1;
                    case 28:
                        if (!((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                            //闰年
                            return 1;
                        }
                    default:
                        return currentDay + 1;
                }
            }
            if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7
                    || currentMonth == 8 || currentMonth == 10 || currentMonth == 12) {
                if (currentDay == 31) {
                    return 1;
                } else {
                    return currentDay + 1;
                }
            } else {
                if (currentDay == 30) {
                    return 1;
                } else {
                    return currentDay + 1;
                }
            }
        } else {
            int dayafter2 = dayAfter(false) + 1;
            if (currentMonth == 2) {
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return dayafter2 == 29 ? dayafter2 : dayafter2 % 29;
                } else {
                    return dayafter2 == 28 ? dayafter2 : dayafter2 % 28;
                }
            }
            if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7
                    || currentMonth == 8 || currentMonth == 10 || currentMonth == 12) {
                return dayafter2 == 31 ? dayafter2 : dayafter2 % 31;
            } else {
                return dayafter2 == 30 ? dayafter2 : dayafter2 % 30;
            }
        }
    }
}
